if (typeof(g_text) == 'undefined') {
	g_text = [];
}
if (typeof(g_base) == 'undefined') {
	g_base = '';
}
if (typeof(g_site) == 'undefined') {
	g_site = '';
}
if (typeof(g_base_site) == 'undefined') {
	g_base_site = '';
}

if (typeof(g_scriptCache) == 'undefined') {
	g_scriptCache = [];
}
/**
 * urls, the script url list
 * function, the callback after the js loaded
 * boolean, runCacheJs, run the js even if the current js file loaded
 */
function getScript() {
	// parameter
	var urls = [];
	var loadedUrls = [];
	var fn = null;
	var runCacheJs = false;
	// init parameter
	for (i = 0 ; i < arguments.length; i++){
		var arg = arguments[i];
		var type = typeof arg;
		if (type == 'function') {
			fn = arg;
		} else if (type == 'string') {
			if (g_scriptCache[arg]) {
				loadedUrls[loadedUrls.length] = arg;
			} else {
				urls[urls.length] = arg;
			}
		} else if (type == 'object') {
			for (var j = 0; j < arg.length; j++) {
				if (g_scriptCache[arg[j]]) {
					loadedUrls[loadedUrls.length] = arg[j];
				} else {
					urls[urls.length] = arg[j];
				}
			}
		} else if (type == 'boolean') {
			runCacheJs = arg;
		}
	}
	
	// process it
	if (runCacheJs) {
		for (var j = 0; j < loadedUrls.length; j++) {
			var script= document.createElement('script');
			script.type= 'text/javascript';
			script.src= loadedUrls[j];
			document.getElementsByTagName("head")[0].appendChild(script);
		}
	}
	if (urls.length > 0) {
		var loadCount = urls.length;
		var reduceLoadCount = function() {
			loadCount--;
			if (loadCount == 0) {
				fn();
			}
		}
		var scriptObjs = document.getElementsByTagName("script");
		
		for (var i = 0; i < urls.length; i++) {
			var url = urls[i];
			g_scriptCache[url] = true;
			
			var found = false;
			for (var j = 0; j < scriptObjs.length; j++) {
				var src = scriptObjs[j].getAttribute('src');
				if (url == src) {
					found = true;
					break;
				}
			};
			
			if (!found || runCacheJs) {
				var script= document.createElement('script');
				script.type= 'text/javascript';
				script.src= url;
				if (fn) {
					script.onreadystatechange = function () {
						if (this.readyState == 'complete') reduceLoadCount();
					}
					script.onload = reduceLoadCount;
				}
				document.getElementsByTagName("head")[0].appendChild(script);
			} else {
				if (fn) {					
					reduceLoadCount();
				}
			}
		}
	} else if (fn) {
		fn();
	}
}

if (typeof(g_cssCache) == 'undefined') {
	g_cssCache = [];
}
String.prototype.endsWith = function(str) {    
	var reg = new RegExp(str+"$");    
	return reg.test(this);       
}
function getCss(url) {
	var key = url;
	
	if (key.endsWith(".tmp.css")) {		// .0.tmp.css, .1.tmp.css --> .tmp.css
		key = key.substring(0, key.length - 10) + ".tmp.css";
	}
	if (!g_cssCache[key]) {
		g_cssCache[key] = true;
		var cssObjs = document.getElementsByTagName("link");
		for (var i = 0; i < cssObjs.length; i++) {
			var href = cssObjs[i].getAttribute('href');
			if (url == href) {
				return;
			}
		};
		
		var css = document.createElement("link");
		css.setAttribute("rel", "stylesheet");
		css.setAttribute("type", "text/css");
		css.setAttribute("href", url);
		document.getElementsByTagName("head")[0].appendChild(css);
	}
}

/**
 * when (var1, var2, ..., fn), when the variable is ready, run the function
 */
function when() {
	var vars = [];
	var fn = null;		// only one function, it'used as callback function
	for (i =0 ; i < arguments.length; i++){
		var arg = arguments[i];
		if (typeof arg == 'function') {
			fn = arg;
		} else {
			if (typeof window[arg] == 'undefined') {
				var expressionList = arg.split('.');
				var checkObj = window;
				for (var j = 0; j < expressionList.length; j++) {
					checkObj = checkObj[expressionList[j]];
					if (!checkObj) {
						break;
					}
				}
				if (!checkObj) {					
					vars[vars.length] = arg;
				}
			}
		}
	}
	if (vars.length == 0) {
		// run the function at once
		fn();
	} else {
		var tryCount = 200;		// 10 seconds
		var time = setInterval(function() {
			var ret = true;
			for (var i = 0; i < vars.length; i++) {
				var arg = vars[i];
				if (typeof window[arg] == 'undefined') {
					var expressionList = arg.split('.');
					var checkObj = window;
					for (var j = 0; j < expressionList.length; j++) {
						checkObj = checkObj[expressionList[j]];
						if (!checkObj) {
							break;
						}
					}
					
					if (!checkObj) {						
						ret = false;
						break;
					}
				}
			}
			
			if (ret || tryCount == 0) {
				clearInterval(time);
				if (ret) {				
					fn();
				}
			} else {
				tryCount--;
			}
		}, 100);
	}
	
}
function addCssNotFlag(name, flag) {
	var objs = document.querySelectorAll('[fullName=' + name + ']');
	if (objs && objs.length > 0) {		
		_addClass(objs[0], flag);
	}
}
function _addClass(obj, clazz) {
	var className = obj.className;
	if (className) {
		obj.className = className + ' ' + clazz;
	} else {
		obj.className = clazz;
	}
	var nodes = obj.childNodes;
	for(var i = 0; i < nodes.length; i++) {
		_addClass(nodes[i], clazz);
	}
}